

-- Namespace
	local _,mod = ...


-- Class lookup
	-- Credit and thanks to Tidy Plates for this.
	local function classcolor(r,g,b)
		r,g,b = floor(r*100+.5),floor(g*100+.5),floor(b*100+.5)
		return ('%03d%03d%03d'):format(r,g,b)
	end
	
	local classes = {}
	
	for class,color in pairs(RAID_CLASS_COLORS) do
		classes[classcolor(color.r,color.g,color.b)] = class
	end
	classes['000100060'] = 'MONK'  -- Rounding error on Blizz's part fucks this up.


-- Name
	mod:event('OnShow',function(nfframe,self)
		local nametext = self.r.nametext
		local name = nametext and nametext:GetText() or ''
		nfframe:Update('name',name)
	end)


-- Level
	mod:event('OnShow',function(nfframe,self)
		local lvltext = self.r.lvltext
		local bossicon = self.r.bossicon
		
		local level = lvltext and tonumber(lvltext:GetText())
		if not level or bossicon:IsShown() then
			level = nil
		end
		
		nfframe:Update('level',level)
	end)


-- Class + Reaction
	mod:event('OnShow',function(nfframe,self)
		local hpbar = self.c.hpbar
		if not hpbar then return end
		
		local type
		
		local r,g,b = hpbar:GetStatusBarColor()
		local R,G,B = ceil(r),ceil(g),ceil(b)
		
		if (R == 1 and G == 0 and B == 0) then
			type = 'hostile'
		elseif (R == 0 and G == 1 and B == 0) or (R == 0 and G == 0 and B == 1) then
			type = 'friendly'
		elseif (R == 1 and G == 1 and B == 0) then
			type = 'docile'
		else
			type = 'enemyplayer'
		end
		nfframe:Update('reaction',type)
		
		if type == 'enemyplayer' then
			local class = classes[classcolor(r,g,b)] or 'unknown'
			nfframe:Update('class',class)
		end
	end)
